// JavaScript Document
$(document).ready(
	function(){
		$('.admins_items_title').click(
			function(){
				$('.admins_items_title').next().slideUp('fast');
				$(this).next().slideToggle('fast');
			}							   
		);
		$('.admins_items_menu a').click(
			function(){
				$('.admins_right').append("<div id='loadingDiv' style='position:absolute;top:5px;left:760px;'><img src='html/images/loading.gif' /></div>");
				$("#admins_frame").css("height","500px");
				setTimeout(function(){$('#loadingDiv').remove()},3000);
			}										  
		);
		$('#admins_frame').load(
			function(){
				$('#loadingDiv').remove();
				commonEffection.resetFrame('admins_frame',false);
			}
		);
		commonEffection.resetFrame('admins_frame',false);
	}				  
);

var commonEffection={
	setLoading:function(){
		var c = $(window.parent.document.body);
		c.find('.admins_right').append("<div id='loadingDiv' style='position:absolute;top:5px;left:760px;'><img src='html/images/loading.gif' /></div>");
		setTimeout(function(){$('#loadingDiv').remove()},10000);
	},
	confirmBack:function(){
		if(confirm("确定放弃本页面返回前一页?")){
			commonEffection.setFrameHeight(500);
			history.back(-1);
		}
	},
	changeFrameHeight:function(offset){
		var height=$(parent.document.getElementById("admins_frame")).css("height");
		$(parent.document.getElementById("admins_frame")).css("height",parseInt(height)+offset+'px');	
	},
	setFrameHeight:function(height){
		$(parent.document.getElementById("admins_frame")).css("height",height+'px');
	},
	resetFrame:function(id,isFrame){
		if(isFrame){
			var bobo=parent.document.getElementById(id);
		}else{
			var bobo=document.getElementById(id);
		}
		if (document.getElementById){
		  if (bobo && !window.opera){
			  if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){
				bobo.height = bobo.contentDocument.body.offsetHeight;
				$(bobo).css('height',bobo.contentDocument.body.offsetHeight+500);
			  }
			  else if(bobo.Document && bobo.Document.body.scrollHeight){
				bobo.height = bobo.Document.body.scrollHeight;
				$(bobo).css('height',bobo.Document.body.scrollHeight+500);
			  }
		  }
		}
	},
	InserImg:function(url){
		if(arguments[1]){
			name=arguments[1];	
		}else{
			name="FCKeditor1";	
		}
		var oEditor = FCKeditorAPI.GetInstance(name);
		oEditor.InsertHtml("<img src='"+url+"'>");
	},
	changePackSell:function(mode,thisInput){
		if(mode=='1'){
			if(!$(thisInput).next().next().is("span")){
				$(thisInput).next().after('<span>&nbsp;( 每包产品 <input type="text" style="width:50px" value="1" name="counts_per_pack" /> 个 ) </span>');
				packCountCheck(new validateProduct());
			}
		}else if(mode=='0'){
			$(thisInput).parent().find('span').remove('span');
		}
	},
	addPrice:function(thisInput){
		$(thisInput).parent().parent().prev().after('<tr><td class="le" style="text-align:center">&nbsp;</td><td class="le" style="text-align:center"><input type="text" name="min_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="max_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="supply_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input readonly="readonly" type="text" name="net_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="delivery_days[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="button" onclick="commonEffection.removeThisTr(this)" value="删除" /></td></tr>');
		commonEffection.resetFrame('admins_frame',true);
		priceCheck(new validateProduct());
	},
	addPattern:function(thisInput){
		$(thisInput).parent().parent().prev().after('<tr><td class="le" style="text-align:center"><input type="text" name="pattern_name[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="min_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="max_counts[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="supply_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input readonly="readonly" type="text" name="net_price[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="text" name="delivery_days[]" style="width:80px;" /></td><td class="le" style="text-align:center"><input type="button" onclick="commonEffection.removeThisTr(this)" value="删除" /></td></tr>');
		commonEffection.resetFrame('admins_frame',true);
		priceCheck(new validateProduct());
	},
	removeThisTr:function(thisInput){
		$(thisInput).parent().parent().remove();
		commonEffection.resetFrame('admins_frame',true);
	},
	removeThisTrAjax:function(thisInput){
		if(!confirm("确定要删除吗?")){
			return false;	
		}
		parent.showMsg('<img src="html/images/loading.gif" />');
		var priceid=$(thisInput).parent().parent().find('input[name="priceid[]"]').val();
		$.post(ROOT_PATH+"/administrator"+CHOSING_DE+"product"+CHOSING_DE+"delpriceajax",{priceid:priceid},function(data) {
			if($('input[name="min_counts[]"]').size()==1){
				thisInput.onclick=function(){};
				$(thisInput).parent().parent().find('input[type!="button"]').val('');
				$(thisInput).parent().parent().find('input[name="priceid[]"]').remove();
			}else{
				$(thisInput).parent().parent().remove();
				commonEffection.resetFrame('admins_frame',true);				
			}
			if(data=="success"){
				parent.shutMsg();
			}else{
				parent.alterMsg('删除失败!');
			}
		});	
	}
}

var fenleiEffection={
	type:'',
	inputid:'flid',
	optionString:'请选择分类',
	showOrHide:function(thisA,flid){
		var smallChildDiv=$(thisA).parent().parent().children('.smallChildDiv');
		if(smallChildDiv.size()==0){
			this.boldOrNormal(thisA,flid);
			commonEffection.resetFrame('admins_frame',true);
		}else{
			smallChildDiv.slideToggle('fast',function(){commonEffection.resetFrame('admins_frame',true);});
		}
	},
	boldOrNormal:function(thisA,flid){
		$(thisA).click(
			function(){
				$('#productLeft a').css('fontWeight','normal');
				$(this).css('fontWeight','bold');
				fenleiEffection.showItems(thisA,flid);
			}
		);			
	},
	showItems:function(thisA,flid){
		$('#productRight').html($(thisA).html());
		productEffection.showAll(1,flid);
	},
	getChildFenlei:function(thisSelect){
		$('#'+fenleiEffection.inputid).val(0);
		$(thisSelect).after('<img id="flloading" style="margin-left:5px" src="html/images/fl_loading.gif" />');
		var pid=$(thisSelect).val();
		if(pid!=0){
			$.post(ROOT_PATH+"/administrator-"+fenleiEffection.type+"-getfenleiajax",{pid:pid},function(data) {
					$('#flloading').remove();
					eval("var flArray="+data);
					if(flArray.length!=0){
						var selectString=' <select onchange="fenleiEffection.getChildFenlei(this);"><option value="0">'+fenleiEffection.optionString+'</option>';
						for(var i=0;i<flArray.length;i++){
							selectString+="<option value="+flArray[i].flid+">"+flArray[i].flname+"</option>";
						}
						selectString+='</select> ';
						fenleiEffection.removeSelect(thisSelect);
						$(thisSelect).after(selectString);
					}else{
						fenleiEffection.removeSelect(thisSelect);
						$('#'+fenleiEffection.inputid).val(pid);
					}
			});	
		}else{
			$('#flloading').remove();
			fenleiEffection.removeSelect(thisSelect);
		}
	},
	removeSelect:function(thisSelect){
		if($(thisSelect).next().is('select')){
			var nextSelect=$(thisSelect).next();
			var removeSelect=new Array();
			removeSelect.push(nextSelect);
			while(nextSelect.next().is('select')){
				nextSelect=nextSelect.next();
				removeSelect.push(nextSelect);
			}
			for(var i in removeSelect){
				removeSelect[i].remove();
			}
		}			
	}
}

var productEffection={
	is_showed:'',
	searchQuery:'',
	is_onsale: -1,
	showAll:function(page,flid){
		$('#productRight').html('<img src="html/images/loading.gif" />');
		$.post(ROOT_PATH+"/administrator-"+fenleiEffection.type+"-getitemajax",{flid:flid,page:page,onsale:this.is_onsale,isshow:this.is_showed,searchQuery:this.searchQuery},function(data) {
			$('#productRight').html(data);
			//productEffection.searchQuery='';
			commonEffection.resetFrame('admins_frame',true);
		});				
	},
	order:{
			setParam:function(){
				var orderid=trim($('input[name="orderid"]').val());
                                var sendid=trim($('input[name="sendid"]').val());
				var time_from=trim($('input[name="time_from"]').val());
				var time_to=trim($('input[name="time_to"]').val());
				var buyer=trim($('input[name="buyer"]').val());
				var seller=trim($('input[name="seller"]').val());
				var money_from=trim($('input[name="money_from"]').val());
				var money_to=trim($('input[name="money_to"]').val());
				var flid2=trim($('select[name="flid"]').val());
				var email_paypal=trim($('input[name="email_paypal"]').val());
				var contact_name=trim($('input[name="contact_name"]').val())
				productEffection.searchQuery='{"orderid":"'+orderid+'","sendid":"'+sendid+'","time_from":"'+time_from+'","time_to":"'+time_to+'","buyer":"'+buyer+'","seller":"'+seller+'","money_from":"'+money_from+'","money_to":"'+money_to+'","flid2":"'+flid2+'","email_paypal":"'+email_paypal+'","contact_name":"'+contact_name+'"}';
			}
	},
	product:{
			setParam:function(){
				var time_from=trim($('input[name="time_from"]').val());
				var time_to=trim($('input[name="time_to"]').val());
				var seller=trim($('input[name="seller"]').val());
				var min_price=trim($('input[name="min_price"]').val());
				var max_price=trim($('input[name="max_price"]').val());
				var keyword=trim($('input[name="keyword"]').val());
				var type=trim($('select[name="type"]').val());
				productEffection.searchQuery='{"time_from":"'+time_from+'","time_to":"'+time_to+'","seller":"'+seller+'","min_price":"'+min_price+'","max_price":"'+max_price+'","type":"'+type+'","keyword":"'+keyword+'"}';
			}
	}
}

var upload={
		type:'',
		formid:'',
		imgurl:'',
		setAppend:function(imgurl){
			return '<div style="margin:10px"><a href="'+imgurl+'" target="_blank"><img style="height:100px;width:100px;vertical-align:middle;border:1px #cccccc dashed" src="'+imgurl+'" /></a> 图片说明:<input type="text" name="imgIntro[]" value="" /><input type="hidden" name="imgUrl[]" value="'+imgurl+'" />　<input type="radio" value="'+imgurl+'" name="cut_url" /> 设为封面 <input name="thumbcaek" type="checkbox" value="1" /> 缩略图　<input type="button" value="删除" onclick="upload.delimg(this);" /></div>';
		},
		uploadimg:function(thisbutton){
			if(!this.checkUploadType($(thisbutton).prev().children().val().split('.').pop().toLowerCase())){
				parent.showMsg('只允许上传如下图片类型: "pjpeg","gif","png","bmp","jpg","jpeg"');
				return false;
			}
			parent.showMsg('<img src="html/images/loading.gif" />');
			var oldAction=$('#'+upload.formid).attr('action');
			var oldTarget=$('#'+upload.formid).attr('target');
			$('#'+upload.formid).attr('action','administrator'+CHOSING_DE+this.type+CHOSING_DE+'uploadimg');
			$('#'+upload.formid).attr('target','productIframe');
			$('#'+upload.formid).submit();
			$('#'+upload.formid).attr('action',oldAction);
			$('#'+upload.formid).attr('target',oldTarget);
			$(thisbutton).parent().find('span').html('<input name="productpic" type="file" id="productpic" />');
		},
		uploadimgmul:function(thisbutton,itemid){
			var a = 0;
			var b = 0;
			
			$(thisbutton).parent().prev().find('input[type="file"]').each(function(){			
				if ($(this).val()!='') {
					if(!upload.checkUploadType($(this).val().split('.').pop().toLowerCase())){
						b = 1;	
					}
					a=1;
				}
			});	
			if (b == 1) {
				parent.showMsg('只允许上传如下图片类型: "pjpeg","gif","png","bmp","jpg","jpeg"');	
				return false;
			}
			if (a==0) {
				parent.showMsg('请选择文件');
				return false;
			}
			parent.showMsg('<img src="html/images/loading.gif" />');
			var oldAction=$('#'+upload.formid).attr('action');
			var oldTarget=$('#'+upload.formid).attr('target');
			$('#'+upload.formid).attr('action','administrator'+CHOSING_DE+this.type+CHOSING_DE+'uploadimgmul?itemid='+itemid);
			$('#'+upload.formid).attr('target','productIframe');
			$('#'+upload.formid).submit();
			$('#'+upload.formid).attr('action',oldAction);
			$('#'+upload.formid).attr('target',oldTarget);
			$(thisbutton).parent().prev().html('<input name="despic[]" type="file" onchange="upload.addparm(this)"/> <br/><br/>');
		},
		addparm: function (obj) {
			$(obj).parent().append('<input name="despic[]" type="file" onchange="upload.addparm(this)"/><input type="button" onclick="upload.delparm(this)" value="删除"/><br/><br/>');
		},
		delparm: function (obj) {
			$(obj).prev().remove();
			$(obj).next().next().remove();
			$(obj).next().remove();
			$(obj).remove();
		},
		setImg:function(imgurl){
			this.imgurl=imgurl;
			$('#productImages').append(this.setAppend(imgurl));
			parent.shutMsg();
			commonEffection.resetFrame('admins_frame',true);
		},
		setImgMul:function(imgurl){
			this.imgurl=imgurl;
			$('#desImages').append('<div style="margin:10px"><a href="'+imgurl+'" target="_blank"><img style="height:100px;width:100px;vertical-align:middle;border:1px #cccccc dashed" src="'+imgurl+'" /></a> <input type="hidden" name="desUrl[]" value="'+imgurl+'" /> <input type="button" value="删除" onclick="upload.delimgMul(this);" /> <input type="button" value="添加" onclick="upload.addImg(this);" /></div>');
			parent.shutMsg();
			commonEffection.resetFrame('admins_frame',true);
		},
		addImg: function(obj) {	
				var url = $(obj).parent().find('input[name="desUrl[]"]').val()
				var Editor = FCKeditorAPI.GetInstance('FCKeditor1'); 
				Editor.InsertHtml("<img src='"+url+"' />");
		},
		checkUploadType:function(type){
			var allowArray=["pjpeg","gif","png","bmp","jpg","jpeg"];
			var isAllow=false;
			for(var i in allowArray){			
				if(type==allowArray[i]){
					isAllow=true;
					break;
				}
			}
			return isAllow;
		},
		delimgbyid:function(thisbutton){
			if(confirm("确定删除该产品图片吗?")){
				parent.showMsg('<img src="html/images/loading.gif" />');
				var attachmentid=$(thisbutton).parent().find('input[name="attachmentid[]"]').val();
				var is_cut_url=$(thisbutton).parent().find('input[type="radio"]').attr('checked')=='true' ? 1 : 0;
				$.post(ROOT_PATH+"/administrator"+CHOSING_DE+upload.type+CHOSING_DE+"delimgbyid",{attachmentid:attachmentid,is_cut_url:is_cut_url},function(data) {
					if(trim(data)=='success'){
						parent.shutMsg('删除成功!');	
						$(thisbutton).parent().remove();
						commonEffection.resetFrame('admins_frame',true);
					}else{
						parent.alterMsg('删除失败!');	
					}
				});	
			}			
		},
		delimgbyidMul:function(thisbutton){
			if(confirm("确定删除该产品图片吗?")){
				parent.showMsg('<img src="html/images/loading.gif" />');
				var attachmentid=$(thisbutton).parent().find('input[name="desid[]"]').val();
				var is_cut_url=$(thisbutton).parent().find('input[type="radio"]').attr('checked')=='true' ? 1 : 0;
				$.post(ROOT_PATH+"/administrator"+CHOSING_DE+upload.type+CHOSING_DE+"delimgbyid",{attachmentid:attachmentid,is_cut_url:is_cut_url},function(data) {
					if(trim(data)=='success'){
						parent.shutMsg('删除成功!');	
						$(thisbutton).parent().remove();
						commonEffection.resetFrame('admins_frame',true);
					}else{
						parent.alterMsg('删除失败!');	
					}
				});	
			}			
		},
		delimg:function(thisbutton){
			if(confirm("确定删除该产品图片吗?")){
				parent.showMsg('<img src="html/images/loading.gif" />');
				var imgurl=$(thisbutton).parent().find('input[name="imgUrl[]"]').val();
				$.post(ROOT_PATH+"/administrator"+CHOSING_DE+upload.type+CHOSING_DE+"delimg",{imgurl:imgurl},function(data) {
					if(data=='success'){
						parent.shutMsg('删除成功!');	
						$(thisbutton).parent().remove();
						commonEffection.resetFrame('admins_frame',true);
					}else{
						parent.alterMsg('删除失败!');	
					}
				});	
			}
		},
		delimgMul:function(thisbutton){
			if(confirm("确定删除该产品图片吗?")){
				parent.showMsg('<img src="html/images/loading.gif" />');
				var imgurl=$(thisbutton).parent().find('input[name="desUrl[]"]').val();
				$.post(ROOT_PATH+"/administrator"+CHOSING_DE+upload.type+CHOSING_DE+"delimg",{imgurl:imgurl},function(data) {
					if(data=='success'){
						parent.shutMsg('删除成功!');	
						$(thisbutton).parent().remove();
						commonEffection.resetFrame('admins_frame',true);
					}else{
						parent.alterMsg('删除失败!');	
					}
				});	
			}
		}
}

var ajaxUpload={
	orderid:'',
	imgurl:'',
	type:'',
	formid:'',
	attachmentid:'',
	setAppend:function(imgurl,attachmentid){
		return '<div style="margin:10px"><a href="'+imgurl+'" target="_blank"><img style="height:100px;width:100px;vertical-align:middle;border:1px #cccccc dashed" src="'+imgurl+'" /></a> <input type="hidden" name="imgUrl[]" value="'+imgurl+'" />　<input type="button" value="删除" onclick="ajaxUpload.delimgbyid(this);" /><input type="hidden" name="attachmentid[]" value="'+attachmentid+'" /></div>';
	},
	uploadimg:function(thisbutton){
		if(!this.checkUploadType($(thisbutton).prev().children().val().split('.').pop())){
			parent.showMsg('只允许上传如下图片类型: "pjpeg","gif","png","bmp","jpg","jpeg"');
			return false;
		}
		parent.showMsg('<img src="html/images/loading.gif" />');
		var oldAction=$('#'+ajaxUpload.formid).attr('action');
		var oldTarget=$('#'+ajaxUpload.formid).attr('target');
		$('#'+ajaxUpload.formid).attr('action','administrator'+CHOSING_DE+this.type+CHOSING_DE+'uploadimg?orderid='+this.orderid);
		$('#'+ajaxUpload.formid).attr('target','productIframe');
		$('#'+ajaxUpload.formid).submit();
		$('#'+ajaxUpload.formid).attr('action',oldAction);
		$('#'+ajaxUpload.formid).attr('target',oldTarget);
		$(thisbutton).parent().find('span').html('<input name="productpic" type="file" id="productpic" />');
	},
	setImg:function(imgurl,attachmentid){
		this.imgurl=imgurl;
		this.attachmentid=attachmentid;
		$('#productImages').append(this.setAppend(imgurl,attachmentid));
		parent.shutMsg();
		commonEffection.resetFrame('admins_frame',true);
	},
	checkUploadType:function(type){
		var allowArray=["pjpeg","gif","png","bmp","jpg","jpeg"];
		var isAllow=false;
		for(var i in allowArray){
			if(type==allowArray[i]){
				isAllow=true;
				break;
			}
		}
		return isAllow;
	},
	delimgbyid:function(thisbutton){
		if(confirm("确定删除该图片吗?")){
			parent.showMsg('<img src="html/images/loading.gif" />');
			var attachmentid=$(thisbutton).parent().find('input[name="attachmentid[]"]').val();
			$.post(ROOT_PATH+"/administrator"+CHOSING_DE+ajaxUpload.type+CHOSING_DE+"delimgbyid",{attachmentid:attachmentid},function(data) {
				if(trim(data)=='success'){
					parent.shutMsg('删除成功!');	
					$(thisbutton).parent().remove();
					commonEffection.resetFrame('admins_frame',true);
				}else{
					parent.alterMsg('删除失败!');	
				}
			});	
		}			
	}
};

var orderExpress={
	kuang:new Object(),
	innerHTML:"",
	mode:"express",
	orderid:'',
	itemid:'',
	shipping_cost:'',
	show:function(){
		this.createKuang();
		this.setKuangContent();
	},
	createKuang:function(){
		var thisOrderKuang=getCenterDiv(700,480,350,240,'thisOrderKuang');
		showBack('orderBackDiv');
		document.getElementsByTagName("body")[0].appendChild(thisOrderKuang);
		$('#thisOrderKuang').fadeIn('slow');
		$('#thisOrderKuang').css('paddingTop','0px');
		this.kuang=thisOrderKuang;
	},
	shutKuang:function(){
		shutBack('orderBackDiv');
		$(this.kuang).remove();
	},
	setKuangContent:function(){
		this.innerHTML='<div class="floatPmTop"><a style="float:right" onclick="orderExpress.shutKuang();">shut</a>订单发货</div>';
		this.innerHTML+='<div class="floatPmCenter"><iframe width="700px" height="450px" src="administrator-order-'+this.mode+'?orderid='+this.orderid+'&itemid='+this.itemid+'&shipping_cost='+this.shipping_cost+'"></iframe></div>';
		this.innerHTML+='<div class="floatPmBottom"></div>';
		$(this.kuang).html(this.innerHTML);
	}
}

